app.controller("bookListCtrl",function($scope,$state,$http,$ionicLoading){
    //用户数据
    var user_info = JSON.parse(localStorage.getItem("UserInfo"));
    // var getPath = JSON.parse(localStorage.getItem("getPath"));
    // var imgPath = JSON.parse(localStorage.getItem("imgPath"));
    $scope.hasMore = true;
    var currentPageNum = 1;
    var totalCount = 0;
    $scope.bookLists = [];
    var obj = {
        userId:user_info.userId,
        currentPage:currentPageNum,
        pageSize:10
    };
    angular.element(document).ready(function () {
        $scope.loadMoreLearnList();
    });

    //加载更多
    $scope.loadMoreLearnList = function(){
        $scope.getBookList();
        obj.currentPage=currentPageNum++;
        $scope.$broadcast('scroll.infiniteScrollComplete');
    };
    //获取学习计划列表
    $scope.getBookList = function () {
        var url = getPath.url+getPath.name+"/app/getBookCycleList";
        $http({
            url: url,
            method: "POST",
            headers: {'Content-Type': 'application/x-www-form-urlencoded'},
            data: obj
        }).success(function (data) {
            var learnListObj = data.resultBean.resultList;
            //获取年月
            var month = [];
            var bookList = [];
            if(learnListObj.length){

                for(var i=0;i<learnListObj.length;i++){
                    var EnglishDateTime = null;
                    var minMonth = learnListObj[i].minTaskDate.substr(5,2)+'月'+learnListObj[i].minTaskDate.substr(8,2)+'日';
                    var maxMonth = learnListObj[i].maxTaskDate.substr(5,2)+'月'+learnListObj[i].maxTaskDate.substr(8,2)+'日';
                    learnListObj[i].minMonth = minMonth;
                    learnListObj[i].maxMonth = maxMonth;
                    switch(learnListObj[i].minTaskDate.substr(5,2)){
                        case '01':
                            EnglishDateTime = 'JANUARY';
                            break;
                        case '02':
                            EnglishDateTime = 'February';
                            break;
                        case '03':
                            EnglishDateTime = 'MARCH';
                            break;
                        case '04':
                            EnglishDateTime = 'APRIL';
                            break;
                        case '05':
                            EnglishDateTime = 'MAY';
                            break;
                        case '06':
                            EnglishDateTime = 'JUNE';
                            break;
                        case '07':
                            EnglishDateTime = 'JULY';
                            break;
                        case '08':
                            EnglishDateTime = 'AUGUST ';
                            break;
                        case '09':
                            EnglishDateTime = 'SEPTEMBER';
                            break;
                        case '10':
                            EnglishDateTime = 'OCTOBER';
                            break;
                        case '11':
                            EnglishDateTime = 'NOVEMBER';
                            break;
                        case '12':
                            EnglishDateTime = 'DECEMBER';
                            break;
                    }
                    learnListObj[i].EnglishMonth = EnglishDateTime;
                };
                for(var i=0;i<learnListObj.length;i++){
                    if(month.indexOf(learnListObj[i].EnglishMonth)==-1){
                        month.push(learnListObj[i].EnglishMonth);
                        var obj = {
                            EnglishMonth:learnListObj[i].EnglishMonth,
                            month:learnListObj[i].cycleMonth,
                            books:[]
                        }
                        bookList.push(obj);
                    }
                }

                for(var i=0;i<month.length;i++){
                    for(var k=0;k<learnListObj.length;k++){
                        if(month[i]==learnListObj[k].EnglishMonth){
                            for(var j=0;j<bookList.length;j++){
                                if(bookList[j].EnglishMonth==learnListObj[k].EnglishMonth){
                                    if(learnListObj[k].bookImg){
                                        learnListObj[k].bookImg = imgPath.url+learnListObj[k].bookImg;
                                    }
                                    bookList[j].books.push(learnListObj[k]);
                                }
                            }
                        }
                    };
                }
                // for(var i=0;i<learnListObj.length;i++){
                //     var EnglishDateTime = null;
                //     var minMonth = learnListObj[i].minTaskDate.substr(5,2)+'月'+learnListObj[i].minTaskDate.substr(8,2)+'日';
                //     var maxMonth = learnListObj[i].maxTaskDate.substr(5,2)+'月'+learnListObj[i].maxTaskDate.substr(8,2)+'日';
                //     learnListObj[i].minMonth = minMonth;
                //     learnListObj[i].maxMonth = maxMonth;
                //     switch(learnListObj[i].minTaskDate.substr(5,2)){
                //         case '01':
                //             EnglishDateTime = 'JANUARY';
                //             break;
                //         case '02':
                //             EnglishDateTime = 'February';
                //             break;
                //         case '03':
                //             EnglishDateTime = 'MARCH';
                //             break;
                //         case '04':
                //             EnglishDateTime = 'APRIL';
                //             break;
                //         case '05':
                //             EnglishDateTime = 'MAY';
                //             break;
                //         case '06':
                //             EnglishDateTime = 'JUNE';
                //             break;
                //         case '07':
                //             EnglishDateTime = 'JULY';
                //             break;
                //         case '08':
                //             EnglishDateTime = 'AUGUST ';
                //             break;
                //         case '09':
                //             EnglishDateTime = 'SEPTEMBER';
                //             break;
                //         case '10':
                //             EnglishDateTime = 'OCTOBER';
                //             break;
                //         case '11':
                //             EnglishDateTime = 'NOVEMBER';
                //             break;
                //         case '12':
                //             EnglishDateTime = 'DECEMBER';
                //             break;
                //     }
                //     learnListObj[i].EnglishMonth = EnglishDateTime;
                //     if(learnListObj[i].bookImg){
                //
                //         learnListObj[i].bookImg = imgPath.url+learnListObj[i].bookImg;
                //     }
                // };
                // totalCount = data.resultBean.pageCond.totalCount;
                Array.prototype.push.apply($scope.bookLists ,bookList);
            }else {
                $scope.hasMore = false;
            }
        }).error(function () {
            $scope.hasMore = false;
            if(($scope.bookLists.length==totalCount)&&(currentPageNum>3)){
                $ionicLoading.show({
                    template: "没有更多内容了！",
                    duration:3000
                });
            }
        });
    }
});